import { setToken, getToken } from '@/utils/auth'
import { login } from '@/api/user'
const state = {
  // token: null
  token: getToken() || null
}

const mutations = {
  setToken: function(state, newToken) {
    // 保存到vuex
    state.token = newToken

    // 保存到cookie
    setToken(newToken)
  }
}

const actions = {
  //
  async login(context, payload) {
    console.log('login', context, payload)

    const res = await login(payload)
    const token = res.data
    // 在action中调用mutation
    context.commit('setToken', token)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

